"  git clone http://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
"  git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
" 
"  Install : vim +PluginInstall +qall
"  Author : derry0316@gmail.com
"  Last change:	2013-12-15

" When started as "evim", evim.vim will already have done these settings.
if v:progname =~? "evim"
  finish
endif

set nocompatible

" allow backspacing over everything in insert mode
set backspace=indent,eol,start

"if has("vms")
  "set nobackup		" do not keep a backup file, use versions instead
"else
  "set backup		" keep a backup file
"endif
set history=50		" keep 50 lines of command line history
set ruler		    " show the cursor position all the time
set showcmd		    " display incomplete commands
set incsearch		" do incremental searching


" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries
" let &guioptions = substitute(&guioptions, "t", "", "g")

" Don't use Ex mode, use Q for formatting
map Q gq

" CTRL-U in insert mode deletes a lot.  Use CTRL-G u to first break undo,
" so that you can undo CTRL-U after inserting a line break.
inoremap <C-U> <C-G>u<C-U>

" In many terminal emulators the mouse works just fine, thus enable it.
"if has('mouse')
  "set mouse=a
"endif

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

" Only do this part when compiled with support for autocommands.
if has("autocmd")

  " Enable file type detection.
  " Use the default filetype settings, so that mail gets 'tw' set to 72,
  " 'cindent' is on in C files, etc.
  " Also load indent files, to automatically do language-dependent indenting.
  filetype plugin indent on

  " Put these in an autocmd group, so that we can delete them easily.
  augroup vimrcEx
  au!

  " For all text files set 'textwidth' to 78 characters.
  autocmd FileType text setlocal textwidth=78

  " When editing a file, always jump to the last known cursor position.
  " Don't do it when the position is invalid or when inside an event handler
  " (happens when dropping a file on gvim).
  " Also don't do it when the mark is in the first line, that is the default
  " position when opening a file.
  autocmd BufReadPost *
    \ if line("'\"") > 1 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif

  augroup END

else

  set autoindent		" always set autoindenting on

endif " has("autocmd")

" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
" Only define it when not defined already.
if !exists(":DiffOrig")
  command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis
		  \ | wincmd p | diffthis
endif

set noswapfile      " Don't create swap files
set number          " Show line number
"set relativenumber  " Line number in relatvie mode.
set numberwidth=4   " Minimal number of columns to use for the line number
" set undofile      " Set no limits to undo
" set nowrap
set wrap            " Wrap when lines longer than the width of window

set guifont=Inconsolata:h12 " Fonts which will be used for GUI version of vim
set iskeyword+=-    " 
autocmd! bufwritepost .vimrc source %   " Auto source .vimrc atfer change it.

syntax on           " Set syntax hightlighting
filetype off        " Disable file type detection

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab       " Use the appropriate number of spaces to insert a <tab>
set smarttab        " <tab> in front of line inserts blanks accroding to 'shiftwidth'

set fileencodings=ucs-bom,utf-8,gb18030,cp936,big5,gb2312
"set encoding=utf-8
set scrolloff=3 "Minimal number of screen lines to keep above or below of cursor
set autoindent
set hidden

set ruler
set cursorline
set cursorcolumn
set ttyfast

" set ignorecase
set smartcase
set showmatch
" subst. all in default
"set gdefault	

" ==========Motion======
" backspace up down space 
set whichwrap=b,s,<,>,[,]
" Motion in Insert mode
inoremap <c-j> <down>
inoremap <c-k> <up>
inoremap <c-h> <left>
inoremap <c-l> <right>

" ======== Hot keys ==========
let mapleader=","
imap jj <esc>
inoremap <F1> <esc>
nnoremap <f1> <esc>
vnoremap <f1> <esc>

nnoremap <tab> %
vnoremap <tab> %
vnoremap <tab> %

" select p
nnoremap <leader>v v`]

" tab switch
nnoremap <leader>t gt
nnoremap <leader>r gT   

" match 
nnoremap / /\v
vnoremap / /\v

" ,<space> to cancel hightlight
nnoremap <leader><space> :nohl<cr>
" ,pwd change to current dir
nmap <leader>pwd :exec "cd" expand("%:h")<cr>
" ,s to search mode
nmap <leader>s :,s///c
" ,p Put vim in paste mode
nmap <leader>p  :set paste<cr>
nmap <leader>pp :set nopaste<cr>

" html/javascript/css/xml

nmap <leader>fx :set ft=xml<cr>
nmap <leader>fm :set ft=mako<cr>
nmap <leader>fh :set ft=html<cr>
" ,ft  fold html tags
nnoremap <leader>ft vatzf
" html comment
nmap <leader>h I//jj
nmap <leader>ch ^xx

" ,w split window in vertical mode
nnoremap <leader>w  <c-w>v<c-w>l 
" ,wc close a split window
nnoremap <leader>wc <c-w>c
" ,ww swicth to another window
nnoremap <leader>ww <c-w>w

" ===============quickfix window====== 
" make 
autocmd FileType c,cpp map <buffer><leader>make :w<cr>:make<cr><cr><cr>:cw 10<cr>
" move to next
nmap <leader>cn :cn<cr>
" move to prev
nmap <leader>cp :cp<cr>
" quick search a word
nmap <leader>lv :lv /<c-r>=expand("<cword>")<cr>/ %<cr> :lw<cr>
nmap <leader>lc :lclose <cr>
nmap <leader>cc :cclose<cr>
nmap <leader>co :copen 10<cr>

" ===========Internal complete ============"
" <c-f> -> filename complete
inoremap <c-f> <c-x><c-f>
" <c-]> -> tags complete
inoremap <c-]> <c-x><c-]>
" <c-l> -> macro complete
inoremap <c-l> <c-x><c-l>
set completeopt=menu        "Disable preview window

" ======== Plugins Management ==========
" Use vundle to manage plugins
set rtp+=~/.vim/bundle/Vundle.vim/
call vundle#rc()
"Bundle 'gmarik/vundle'
Plugin 'gmarik/Vundle.vim'

" =========Taglist=========
Bundle 'taglist.vim'
let Tlist_Ctags_Cmd = 'ctags'
let Tlist_Show_One_File = 1
let Tlist_Exit_OnlyWindow = 1
let Tlist_Use_Right_Window = 0
map <silent> <f9> :TlistToggle<cr> 
" Use to auto generate tags file
function! GenerateTagsFile()
    let dir=getcwd()
    " delete old tags file
    if filereadable("tags")
        let fdel=delete("./"."tags")
        if (fdel != 0)
            echohl WarningMsg | echo "delete tags error" | echohl None
            return
        endif
    endif
    " delete cscope.files
    if filereadable("cscope.files")
        let fdel=delete("./"."cscope.files")
        if (fdel != 0)
            echohl WarningMsg | echo "delete cscope.files error" | echohl None
            return 
        endif
    endif
    if filereadable("cscope.out")
        let fdel=delete("./"."cscope.out")
        if (fdel != 0)
            echohl WarningMsg | echo "delete cscope.out error" | echohl None
            return 
        endif
    endif
    if executable("ctags")
        silent! execute "!ctags -R --c-types=+p --fields=+S *"
        silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."
    endif
    if executable("cscope")
        silent! execute "!find . -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.hpp' -o -name '*.cc' -o -name '*.cxx' > cscope.files"
        silent! execute "!cscope -bq cscope.files"
        execute "normal :"
        if filereadable("cscope.out")
            execute "cs add cscope.out"
        endif
    endif
endfunction
" Use <F12> to auto generate tags file
map <silent> <f12> :call GenerateTagsFile()<cr>

" ===========SuperTab============
let g:SupperTabRetainCompletionType=1   " Remember complete type
let g:SupperTabDefaultCompleteionType="<c-x><c-o>"

" ===========winmanager===========
Bundle 'winmanager'
let g:winManagerWindowLayout = 'BufExplorer,FileExplorer|TagList'
let g:winManagerWidth = 25
let g:bufExplorerMaxHeight = 30
let g:defaultExplorer = 0
nmap <silent> <f8> :WMToggle<cr>

" ==========bufexplorer===========
Bundle 'bufexplorer.zip'

" ======AutoClose==============
Bundle 'AutoClose'

" ========EasyMotion============
" <leader><leader>w to open a motion marks"
Bundle 'EasyMotion'
let g:EasyMotion_leader_key = '<Leader><Leader>'

" ====== NerdTree==============
Bundle 'The-NERD-tree'
"nmap <leader>nt :NERDTree<cr>:set rnu<cr>
nmap <leader>nt :NERDTreeToggle<cr>:set rnu<cr>
let NERDTreeShowBookmarks=1
let NERDTreeShowFiles=1
let NERDTreeIgnore=['\.$','\~$']
let NERDTreeShowLineNumbers=1
let NERDTreeWinPos=1

" NERDTree_commenter
Bundle 'The-NERD-Commenter'
let NERDShutUp=1
"Use <ctrl-h> to comment or uncoment
map <c-h> ,c<space>   

"===========AutoComplPop========="                                                      
Bundle 'AutoComplPop'                                                                   

"========OmniCppComplete========="
Bundle 'OmniCppComplete' 
set nocp "nocompatiable" 
set completeopt=menu,menuone 
let OmniCpp_MayCompleteDot=1 "for." 
let OmniCpp_MayCompleteArrow=1 "for->" 
let OmniCpp_MayCompleteScope=1 "for::" 
let OmniCpp_GlobalScopeSearch=0 "search namespace"
let OmniCpp_NamespaceSearch=0   "global search"
let OmniCpp_ShowPrototypeInAbbr=1 "show function prototype"
let OmniCpp_SelectFirstItem=2 "select first item default"

"=================Color scheme set==========="
Bundle 'Solarized'
Bundle 'molokai'
Bundle 'Calm-Breeze'
Bundle 'eclipse.vim'
set t_Co=256
set colorcolumn=85
set modelines=0
set background=dark

" let g:solarized_termtrans=1
" let g:solarized_termcolors=256
" let g:solarized_contrast="high"
" let g:solarized_visibility="high"
"colorscheme solarized

"colorscheme eclipse
let g:molokai_original=1
colorscheme molokai

"colorscheme eclipse

" ===========DoxygenToolkit======== "
Bundle 'DoxygenToolkit.vim'
"DoxLic,DoxAuthor,Dox"
let g:DoxygenToolkit_authorName="derry, derry0316@gmail.com"
let g:DoxygenToolkit_licenseTag="Copyright (C) ".strftime("%Y")." derry,  All Rights Reserved."
let g:DoxygenToolkit_briefTag_pre="@brief\t"
let g:DoxygenToolkit_paramTag_pre="@param\t"
let g:DoxygenToolkit_maxFunctionProtoLines=30
let g:DoxygenToolkit_returnTag="@return\t"
let g:DoxygenToolkit_briefTag_funcName="yes"
let g:DoxygenToolkit_compactOneLineDoc="no"
let g:DoxygenToolkit_undocTag="DOXIGEN_SKIP_BLOCK"
"let g:DoxygenToolkit_blockHeader="==============================================="
"let g:DoxygenToolkit_blockFooter="==============================================="
map fl : DoxLic<cr> 
map fa : DoxAuthor<cr>
map fg : Dox<cr>

"======support for Qt class ====="
Bundle 'cpp.vim'
"======support stl reference====="
Bundle 'stlrefvim'
Bundle 'STL-improved'
Bundle 'git://github.com/vhdirk/vim-cmake.git'

call vundle#end()

filetype plugin indent on
